package ldwu.spark.mytest;

import java.sql.{Connection, DriverManager, ResultSet};
/**
 * Created by liangdong on 2016/1/29.
 */
object MySqlDbManager {
  def main(args: Array[String]){
    excut_data()
  }
  def getdata(): Unit = {
    // Change to Your Database Config
    val conn_str = "jdbc:mysql://localhost:3306/myhome?user=root&password=root"

    // Load the driver
    classOf[com.mysql.jdbc.Driver]

    // Setup the connection
    val conn = DriverManager.getConnection(conn_str)
    try {
      // Configure to be Read Only
      val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)

      // Execute Query
      val rs = statement.executeQuery("SELECT  id,  first_name,  last_name,  email FROM myhome.mytest_user_test;")

      // Iterate Over ResultSet
      while (rs.next) {
        println(rs.getString("first_name"))
      }
    }
    finally {
      conn.close
    }
  }
  def excut_data(): Unit ={
    // Change to Your Database Config
    val conn_str = "jdbc:mysql://localhost:3306/myhome?user=root&password=root"

    // Load the driver
    classOf[com.mysql.jdbc.Driver]
    val conn = DriverManager.getConnection(conn_str)
    val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)

    // do database insert
    try {
      val prep = conn.prepareStatement("INSERT INTO myhome.mytest_user_test (first_name,last_name,email) VALUES (?, ?, ?) ")
      prep.setString(1,"xiao")
      prep.setString(2, "静")
      prep.setString(3, "xiaox@qq.com")
      prep.executeUpdate
    }
    finally {
      conn.close
    }
  }

}
